<script setup>
import { ref } from 'vue'

const dialogVisible = ref(false)

const formModel = ref({
  cate_name: '',
  cate_alias: ''
})

const rules = {
  cate_name: [
    { required: true, message: '请输入分类名称', trigger: 'blur' }, //最后一句是指失焦时校验,change是改变校验
    {
      pattern: /^\S{1,10}$/,
      message: '必须是 1-10位非空的字符',
      trigger: 'blur'
    }
  ],
  cate_alias: [
    { required: true, message: '请输入分类别名', trigger: 'blur' },
    {
      //正则校验
      pattern: /^[a-zA-Z0-9]{1,15}$/,
      message: '密码必须是1-15位字母或者数字',
      trigger: 'blur'
    }
  ]
}

const open = (row) => {
  dialogVisible.value = true
  formModel.value = { ...row }
}

defineExpose({
  open
})
</script>
<template>
  <el-dialog
    v-model="dialogVisible"
    :title="formModel.id ? '编辑分类' : '添加分类'"
    width="30%"
    height="100"
  >
    <el-form :model="formModel" :rules="rules">
      <el-form-item prop="cate_name" label="分类名称">
        <el-input
          v-model="formModel.cate_name"
          placeholder="请输入分类名称"
        ></el-input>
      </el-form-item>
      <el-form-item prop="cate_alias" label="分类别名"
        ><!--label 加到item上-->
        <el-input
          v-model="formModel.cate_alias"
          placeholder="请输入分类别名"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogVisible = false">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
